package cn.com.farsgiht.bluetoothdemo.protocol;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.R.integer;
import android.util.Log;

/**
 * 
 * @author Micheal
 *
 */
public class DataProtocol {
	public final static byte HEAD = 0xA;
	public final static byte TYPE_MSG = 0xC;
	public final static byte TYPE_FILE = 0xF;
	public final static byte TYPE_UPLOUDFTLE=0xE;
		
	
	
	public static byte[] packMsg(String msg) throws UnsupportedEncodingException{
		byte[] msgbytes = msg.getBytes("UTF-8");
		byte lowLen = (byte)(msgbytes.length & 0xFF);
		byte hiLen = (byte)(msgbytes.length >> 8 & 0xFF);
		byte[] buf = new byte[msgbytes.length + 4];
		buf[0] = HEAD;
		buf[1] = TYPE_MSG;
		buf[2] = hiLen;
		buf[3] = lowLen;
/**
		System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。 
	其函数原型是：
	public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)1
	src:源数组； 
	srcPos:源数组要复制的起始位置； 
	dest:目的数组； 
	destPos:目的数组放置的起始位置； 
	length:复制的长度。 
	注意：src and dest都必须是同类型或者可以进行转换类型的数组．
*/
		//生成要传输的buf
		System.arraycopy(msgbytes, 0, buf, 4, msgbytes.length);
		return buf;
	}
	
	public static byte[] packFile(File file) throws UnsupportedEncodingException{
		byte total0 = (byte)(file.length() & 0xFF);
		byte total1 = (byte)(file.length() >> 8  & 0xFF);
		byte total2 = (byte)(file.length() >> 16 & 0xFF);
		byte total3 = (byte)(file.length() >> 24 & 0xFF);
		
		byte[] fnamebytes = file.getName().getBytes("UTF-8");

		byte lowLen = (byte)(fnamebytes.length & 0xFF);
		byte hiLen = (byte)(fnamebytes.length >> 8 & 0xFF);
		
		byte[] buf = new byte[fnamebytes.length + 8];
		buf[0] = HEAD;
		buf[1] = TYPE_FILE;
		buf[2] = total3;
		buf[3] = total2;
		buf[4] = total1;
		buf[5] = total0;
		buf[6] = hiLen;
		buf[7] = lowLen;

		System.arraycopy(fnamebytes, 0, buf, 8, fnamebytes.length);
		return buf;
	}
	
	public static byte[]  uplodFile(File file) throws IOException {
		FileInputStream fileInputStreamn=new FileInputStream(file) ;
		int len=0;
		byte[] b=new byte[1024];
		int i=0;
		while ((len=fileInputStreamn.read(b))!=-1) {
			i++;
			Log.e("uplodFile", "读取文件"+i);
			byte lowLen = (byte)(b.length & 0xFF);
			byte hiLen = (byte)(b.length >> 8 & 0xFF);
			byte[] buf=new byte[b.length+4];
			buf[0]=HEAD;
			buf[1]=TYPE_UPLOUDFTLE;
			buf[2]=hiLen;
			buf[3]=lowLen;
			System.arraycopy(b, 0, buf, 4, b.length);
			//返回读取文件的字节流（包）,
			return b;
		}
		fileInputStreamn.close();	
		return null;
	}
	public static Message unpackData(byte[] data) throws UnsupportedEncodingException{
		if(data[0] != HEAD)
			return null;
		Message msg = new Message();
		switch(data[1]){
		case TYPE_FILE:
			msg.type = TYPE_FILE;
			msg.total = data[2] << 24 | data[3] << 16 | data[4] << 8 | data[5];
			msg.fileName = new String(data, 8, data[6] << 8 | data[7], "UTF-8");
			break;
		case TYPE_MSG:
			msg.type = TYPE_MSG;
			msg.length = data[2] << 8 | data[3];
/**
		public String (Byte[] byte,int offset，int length,String CharsetName)
		byte 要解码的字符byte
		offset 要解码的第一个字符索引
		length 要解码的byte个数
		CharsetName 编码格式，"UTF-8"	 
*/			
			msg.msg = new String(data, 4, msg.length, "UTF-8");
			break;
		case TYPE_UPLOUDFTLE:
			msg.type=TYPE_UPLOUDFTLE;
			msg.length = data[2] << 8 | data[3];
			msg.fileDate=new byte[msg.length];
			//得到了发来的文件包
			System.arraycopy(data, 4, msg.fileDate, 0, msg.length);
		}
		return msg;
	}
}
